﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.SystemModule;
using DomainComponents.Common;

namespace DomainComponents.XCRM {
    public enum LeadStatus {
        None = 0,
        Lost = 1,
        [DisplayName("Cannot Contact")]
        CannotContact = 2,
        [DisplayName("No Longer Interested")]
        NoLongerInterested = 3,
        Canceled = 4,
        Qualified = 5
    }

    public enum LeadSource {
        ColdCall,
        ExistingCustomer,
        SelfGenerated,
        EmployeeReferral,
        Partner,
        PublicRelations,
        Seminar,
        TradeShow,
        Web,
        WordOfMouth,
        Advertisement,
        Other
    }

    public class LeadValidationRules {
        public const string TopicIsRequired = "TopicIsRequired";
        public const string CompanyNameIsRequired = "CompanyNameIsRequired";
        public const string LastNameIsRequired = "LeadLastNameIsRequired";
    }

    [DomainComponent]
    public interface ILeadTarget {
        [VisibleInListView(false)]
        ILead SourceLead { get; set; }
    }

    [DomainComponent]
    [VisibleInReports]
    [ImageName("BO_Lead")]
    [Appearance("Disable ILead by Status", TargetItems = "*", Criteria = "Status != 'None'", Enabled = false)]
    [XafDefaultProperty("Name")]
    //[ListViewFilter("Lead_OpenLeads", "[Status] = 'None'", "Open Leads", true, Index = 0)]
    //[ListViewFilter("Lead_ClosedLeads", "[Status] != 'None'", "Closed Leads", Index = 2)]
    public interface ILead {
        [Calculated("isnull(FirstName, '') + ' ' + isnull(LastName, '')")]
        string Name { get; }

        [RuleRequiredField(LeadValidationRules.TopicIsRequired, DefaultContexts.Save)]
        [VisibleInLookupListView(true)]
        string Topic { get; set; }

        LeadStatus Status { get; set; }

        DateTime CreatedOn { get; set; }

        [RuleRequiredField(LeadValidationRules.CompanyNameIsRequired, DefaultContexts.Save)]
        [VisibleInListView(false)]
        string CompanyName { get; set; }
        
        [RuleRequiredField(LeadValidationRules.LastNameIsRequired, DefaultContexts.Save)]
        [VisibleInListView(false)]
        string LastName { get; set; }

        [VisibleInListView(false)]
        string FirstName { get; set; }

        [VisibleInListView(false)]
        LeadSource LeadSource { get; set; }

        [VisibleInListView(false)]
        IDCUser Owner { get; set; }
        
        [VisibleInListView(false)]
        string JobTitle { get; set; }

        [VisibleInListView(false)]
        string WebSite { get; set; }

        [VisibleInListView(false)]
        decimal AnnualRevenue { get; set; }

        [VisibleInListView(false)]
        string NumberOfEmployees { get; set; }

        [VisibleInListView(false)]
        string SICCode { get; set; }

        [VisibleInListView(false)]
        PreferredContactMethod PreferredContactMethod { get; set; }

        [Action(PredefinedCategory.RecordEdit, Caption = "Disqualify Lead...", AutoCommit = true,
            TargetObjectsCriteria = "Status = 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Disqualify(DisqualifyLeadParameters paremeters);
        
        [Action(PredefinedCategory.RecordEdit, Caption = "Reactivate Lead...", AutoCommit = true,
            TargetObjectsCriteria = "Status != 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Reactivate();
        
        void CopyTo(ILeadTarget leadTarget);
    }

    [DomainComponent]
    public interface IGenericEmail {
        [VisibleInListView(false)]
        string Email { get; set; }
        void Copy(IGenericEmail target);
    }
    [DomainLogic(typeof(IGenericEmail))]
    public class GenericEmailLogic {
        public static void Copy(IGenericEmail source, IGenericEmail target) {
            if(target != null) {
                target.Email = source.Email;
            }
        }
    }
    [DomainLogic(typeof(ILead))]
    public class LeadLogic {
        public static void AfterConstruction(ILead lead, IObjectSpace os) {
            lead.CreatedOn = DateTime.Now;
        }
        public static void Reactivate(ILead lead) {
            lead.Status = LeadStatus.None;
        }
        public static string Get_DisplayName(ILead lead) {
            return lead.Name; 
        }
        public static void CopyTo(ILead lead, ILeadTarget leadTarget) {
            if(leadTarget != null) {
                ((ILeadTarget)leadTarget).SourceLead = lead;
            }
        }
    }
}
